﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProgrammersCalculator.Interface;

namespace ProgrammersCalculator.Model
{
    public class HashAlgorithmWrapper : IAlgorithm
    {
        public static string ByteArrayToString(byte[] ba)
        {
            string hex = BitConverter.ToString(ba);
            return hex.Replace("-", "");
        }
        public string ComputeHash(string input, ProgrammersCalculator.Common.AlgorithmType algoType)
        {
            string hash = string.Empty;
            System.Security.Cryptography.HashAlgorithm algo = null;
            var byteArray = Encoding.ASCII.GetBytes(input);
            switch (algoType)
            {
                case ProgrammersCalculator.Common.AlgorithmType.MD5:
                    algo = System.Security.Cryptography.HashAlgorithm.Create("MD5");
                    hash = ByteArrayToString(algo.ComputeHash(byteArray));

                    break;
                case ProgrammersCalculator.Common.AlgorithmType.SHA1:
                    algo = System.Security.Cryptography.HashAlgorithm.Create("sha1");
                    hash = ByteArrayToString(algo.ComputeHash(byteArray));
                    break;
                case ProgrammersCalculator.Common.AlgorithmType.SHA128:
                    algo = System.Security.Cryptography.HashAlgorithm.Create("sha128");
                    hash = ByteArrayToString(algo.ComputeHash(byteArray));
                    break;
                case ProgrammersCalculator.Common.AlgorithmType.SHA256:
                    algo = System.Security.Cryptography.HashAlgorithm.Create("sha256");
                    hash = ByteArrayToString(algo.ComputeHash(byteArray));
                    break;
                case ProgrammersCalculator.Common.AlgorithmType.SHA512:
                    algo = System.Security.Cryptography.HashAlgorithm.Create("sha512");
                    hash = ByteArrayToString(algo.ComputeHash(byteArray));
                    break;
                default:
                    break;
            }
            return hash;
        }
    }
}
